/*
 * @Author: sunzhijun 
 * @Date: 2019-03-26 17:29:09 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2019-12-10 20:52:40
 */

import React, { Component } from 'react';
import { base, high, ajax, toast, promptBox } from 'nc-lightapp-front';
import { formatDate } from '../../../../hrpub/common/utils/utils.js';
import deepCopy from 'src/hrpub/common/utils/deep-copy';
const { NCDatePicker } = base;
const { NCModal, NCButton } = base;
const { Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter } = NCModal;
// import zhCN from "rc-calendar/lib/locale/zh_CN";
// import enUS from "rc-calendar/lib/locale/en_US";
import './index.less';

class ChangePersonnel extends Component {
	constructor(props) {
		super(props);
		this.state = {
			tableid: 'chgPsnList',
			pk_org: props.pkOrg,
			title: '', //默认弹窗名称
			agreementType: props.agreementType, //协议类型
			startTime: formatDate(new Date(new Date().setMonth(new Date().getMonth() - 1))), //设置默认时间
			endTime: formatDate(new Date()) //设置默认时间后前一个月
		};
		this.cancelModal = this.cancelModal.bind(this);
		this.sureSelectData = this.sureSelectData.bind(this);
	}

	componentDidMount() {
		this.getInitdata(0);
	}

	//确定
	sureSelectData() {
		let selectData = this.props.editTable.getCheckedRows(this.state.tableid);
		let pk_psnjobArr = [];
		selectData &&
			selectData.map((v, i) => {
				pk_psnjobArr.push(v.data.values.pk_psnorg.value);
			});
		this.props.getSearchData && this.props.getSearchData(pk_psnjobArr);
	}

	//取消
	cancelModal() {
		this.props.cancelChangeModal && this.props.cancelChangeModal();
	}

	//开始时间
	changeStartDate(time) {
		this.setState({
			startTime: time
		});
	}

	//结束时间
	changeEndDate(time) {
		this.setState({
			endTime: time
		});
	}

	//查询数据
	queryData() {
		this.getInitdata(1);
	}

	//初始化和查询数据
	getInitdata(dataType) {
		let { startTime, endTime, pk_org, agreementType } = this.state;
		ajax({
			url: '/nccloud/hrcm/common/QueryChgPsnAction.do',
			data: {
				dataType: dataType || 0,
				begindate: startTime,
				enddate: endTime,
				pk_org: pk_org,
				agreementType: agreementType
			},
			success: (res) => {
				if (res.success && res.data) {
					if (!dataType && !!res.data.data) {
						let data = res.data.data;
						let title = data.title;
						let psnListData = data.template.chgPsnList;
						let template = {
							chgPsnList: psnListData
						};
						this.props.meta.addMeta(template);
						this.setState({
							tableid: 'chgPsnList',
							title: title
						});
					} else if (!!dataType && res.data.data) {
						let data = deepCopy(res.data.data);
						let rows = data.chgPsnList.rows;
						this.props.editTable.setTableData(this.state.tableid, { rows: rows });
						this.props.editTable.selectAllRows(this.state.tableid, true); //设置全选
						this.forceUpdate()
					} else {
						let data = res.data;
						let msg = data.msg;
						this.promptBoxModal(msg);
					}
				}
			}
		});
	}

	//确认弹窗
	promptBoxModal(msg) {
		let { json } = this.props;
		promptBox({
			color: 'warning', // 提示类别默认"success"， "success"/"info"/"warning"/"danger",非必输
			title: '', // 提示标题, 默认不同类别下分别为："已成功"/"帮助信息"/"请注意"/"出错啦",非必输/* 国际化处理： 确认删除*/
			content: msg, // 提示内容,非必输/* 国际化处理： 您确定要删除所选数据么?*/
			noFooter: false, // 是否显示底部按钮(确定、取消),默认显示(false),非必输
			noCancelBtn: true, // 是否显示取消按钮,，默认显示(false),非必输
			beSureBtnName: json['hrcm-000006'], // 确定按钮名称, 默认为"确定",非必输/* 国际化处理： 确定*/
			hasCloseBtn: true, //显示“X”按钮，默认不显示，不显示是false，显示是true
			closeByClickBackDrop: false //点击遮罩关闭提示框，默认是true点击关闭，阻止关闭是false
		});
	}

	render() {
		let { editTable, isVisible, json } = this.props;
		let { createEditTable } = editTable;
		let { title, tableid, startTime, endTime } = this.state;
		return (
			<NCModal
				show={isVisible}
				onHide={this.cancelModal.bind(this)}
				zIndex="204"
				backdrop="static"
				dialogClassName="modal-height"
			>
				<NCModalHeader closeButton={true}>{title}</NCModalHeader>
				<NCModalBody>
					<div className="date-header">
						<div className="date-picker">
							<NCDatePicker value={startTime} onChange={this.changeStartDate.bind(this)} />
						</div>
						<div className="date-picker">
							<NCDatePicker value={endTime} onChange={this.changeEndDate.bind(this)} />
						</div>
						<div className="query-btn">
							<NCButton onClick={this.queryData.bind(this)}>
								{json['hrcm-000042']}
								{/* 查询 */}
							</NCButton>
						</div>
					</div>
					<div className="nc-singleTable-table-area">
						{createEditTable(tableid, {
							cancelCustomRightMenu: true,
							showIndex: true, //显示序列号
							showCheck: true //显示复选框
						})}
					</div>
				</NCModalBody>
				<NCModalFooter>
					<NCButton onClick={this.sureSelectData}>
						{/* 确定 */}
						{json['hrcm-000006']}
					</NCButton>
					<NCButton onClick={this.cancelModal}>
						{/* 取消 */}
						{json['hrcm-000031']}
					</NCButton>
				</NCModalFooter>
			</NCModal>
		);
	}
}
export default ChangePersonnel;
